<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function namespace(oNamespace, sPackage) {
    var str = sPackage;
    var arr = str.split('.');
    var len = arr.length;

    // {arr[0]:{text1,arr[1]:{arr[2]:{arr[3]:{}}}}}
    var obj = oNamespace; //保留对原始对象的引用

    for(var i = 0; i < len; i++){
        if(arr[i] in oNamespace){ //空间名在对象中
            if(typeof oNamespace[arr[i]] != 'object'){ //为原始值
                oNamespace[arr[i]] = {}; //将此属性设置为空对象
            }
        }else{  //空间名不在对象中，建立此属性，赋值为空
            oNamespace[arr[i]] = {};
        }

        oNamespace = oNamespace[arr[i]];
    }
    return obj;
}
       let hello = namespace({a: {test: 1, b: 2}}, 'a.b.c.d')
       console.log(hello)
    </script>
</body>
</html>